import comm from "@/common/common"
import $store from '../store'

class user{
	//获取用户缓存信息
	getUser($refresh=false){
		if($refresh){
			return new Promise((resolve, reject) => {	
				uni.$u.api.userInfo().then(res=>{
					$store.commit('login',res.data)
					resolve(res.data.memeber)
				})
			})
		}else{
			return $store.state.$userInfo||''
		}
	}
	
	login(type){
		return new Promise((resolve, reject) => {	
			if(type==="weixin"){
				this.wxLogin().then(res=>{
					resolve(res)
				}).catch(e=>{
					reject(e)
				})
			}else if(type==="alipay"){
				this.alipayLogin().then(res=>{
					resolve(res)
				}).catch(e=>{
					reject(e)
				})
			}
		})
	}
	
	wxLogin(){
		return new Promise((resolve, reject) => {	
			uni.showLoading({
				title: '获取中'
			});
			let self=this
			uni.getUserProfile({
				desc: '用于注册会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: (detail) => {
					self.getLoginConfig(function(data){
						uni.$u.api.wxLogin(data).then(res=>{
							uni.hideLoading()
							resolve(res.data);
						})
					},detail)
				},
				fail: (error) => {
					if(error.errno==112){
						comm.info('请在隐私条例添加接口权限！');
					}else{
						comm.info('需授权才可以登陆！');
					}
					// uni.hideLoading()
				}
			});
		})
	}
	
	//支付宝登录
	alipayLogin(){
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '获取中'
			});
			let self=this
			uni.login({
				provider: 'alipay',
				scopes:'auth_base',
				success: (codeData) => {
					uni.getUserInfo({
						provider: 'alipay',
						success:(infoRos)=>{
							let data={
								code:codeData.code,
								info:JSON.stringify(infoRos.userInfo),
							}
							uni.$u.api.aliappLogin(data).then(res=>{
								uni.hideLoading()
								resolve(res.data);
							})
						},fail:(e)=>{
							uni.hideLoading()
							console.log(e)
							reject(e)
						}
					})
				},fail: (e) => {
					uni.hideLoading()
					console.log(e)
					comm.info(e)
					reject(e)
				}
			});
		})
	}
	
	//获取微信手机号码
	getWxMobile(e){		
		if (e.detail.errMsg == "getPhoneNumber:ok"){
			e=e.detail;
			let than = this;
			this.getLoginConfig(function(code){
				uni.$u.api.getMobile({encryptedData:e.encryptedData,iv:e.iv}).then(res => {
					if(than.comm.isok(res,true)){
						if(res.data.phoneNumber ==''){
							than.comm.error('获取用户手机号码异常');
						}else{
							console.log(res.data.phoneNumber);
							than.$u.api.bindMobile({mobile:res.data.phoneNumber}).then(res =>{
								if(than.comm.isok(res,true)){
									than.comm.restUser();
									than.comm.toBack();
								}
							})
						}
					}
				})
			})
		}
	}
	
	//获取登陆参数
	getLoginConfig(cb,e){
		var than = this;
		uni.login({
			success: function (res) {
				if(res.errMsg=='login:ok'){
					comm.F('user_code',res.code)
					var data={
						code			: res.code,
						encryptedData	: e.encryptedData,
						iv				: e.iv,
						rawData			: e.rawData,
						signature		: e.signature,
					}
					typeof cb == "function" && cb(data);
					//console.log(data);
				}
			},
			fail: function () {
				uni.showModal({
					title: '获取信息失败',
					content: '请允许授权以便为您提供给服务',
				})
			}
		});
	}
}
export default new user();